package com.fa.dv.service.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.fa.common.utils.SecurityUtils;
import com.fa.dv.domain.MachineryType;
import com.fa.dv.mapper.MachineryTypeMapper;
import com.fa.dv.service.IMachineryTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
public class MachineryTypeServiceImpl implements IMachineryTypeService {

    @Autowired
    private MachineryTypeMapper machineryTypeMapper;


    @Override
    public  List<Tree<String>> tree() {
        // 查询所有设备类型
        List<MachineryType> nodeList = machineryTypeMapper.selectAll();
        System.out.println(nodeList);
        //
        //配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
// 自定义属性名 都有默认值的
       //treeNodeConfig.setWeightKey("orderNum");
        treeNodeConfig.setIdKey("machineryTypeId");

//转换器 (含义:找出父节点为字符串零的所有子节点, 并递归查找对应的子节点, 深度最多为 3)
        List<Tree<String>> treeNodes = TreeUtil.build(nodeList, "0", treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(Objects.toString(treeNode.getMachineryTypeId(), ""));
                    tree.setParentId(Objects.toString(treeNode.getParentTypeId(), "0"));
                    //tree.setWeight(treeNode.getOrderNum());
                    // 扩展属性 ...
                    tree.putExtra("enableFlag", treeNode.getEnableFlag());
                    tree.putExtra("createTime", treeNode.getCreateTime());
                    tree.putExtra("machineryTypeCode", treeNode.getMachineryTypeCode());
                    tree.putExtra("machineryTypeName", treeNode.getMachineryTypeName());
                    tree.putExtra("remark", treeNode.getRemark());
                });
        return treeNodes;
    }

    @Override
    public List<MachineryType> selectMachineryTypeInfo(MachineryType machineryType) {
        return machineryTypeMapper.selectMachineryTypeInfo(machineryType);
    }

    @Override
    public void insert(MachineryType machineryType) {
        //machineryTypeCode每次都要加一
        //先查出最后一个machineryTypeCode
       MachineryType lastMachineryType = machineryTypeMapper.selectLastMachineryTypeCode();
        String machineryTypeCode = lastMachineryType.getMachineryTypeCode();
        String[] s = machineryTypeCode.split("_");
        int lastCodeNum = Integer.parseInt(s[2]);
        lastCodeNum +=1;
        StringBuffer sb = new StringBuffer("M_TYPE_");
        int length = String.valueOf(lastCodeNum).length();
        if(length ==1){
            sb.append("000");
        }else if (length ==2){
            sb.append("00");
        }else if (length ==3){
            sb.append("0");
        }
        sb.append(lastCodeNum);
        String newMachineryTypeCode = sb.toString();

        //ancestors
        //先查出要添加的这个单位的父级ancestors,后再加上父级
        MachineryType parentMachineryType = machineryTypeMapper.selectParent(machineryType.getParentTypeId());
        // 拿到父级的ancestors再加上父级的machinery_type_id
        String ancestors = parentMachineryType.getAncestors() + "," +parentMachineryType.getMachineryTypeId() ;

        machineryType.setAncestors(ancestors);
        machineryType.setMachineryTypeCode(newMachineryTypeCode);
        machineryType.setCreateBy(SecurityUtils.getUsername());
        machineryType.setUpdateBy(SecurityUtils.getUsername());
        machineryType.setCreateTime(new Date());
        machineryType.setUpdateTime(new Date());
        System.out.println("这是新增的"+machineryType);
        machineryTypeMapper.insert(machineryType);
    }

    @Override
    public MachineryType selectParent(int parentTypeId) {
        return machineryTypeMapper.selectParent(parentTypeId);
    }

    @Override
    public void update(MachineryType machineryType) {
        machineryType.setUpdateBy(SecurityUtils.getUsername());
        machineryType.setUpdateTime(new Date());
        machineryTypeMapper.update(machineryType);
    }

    @Override
    public void delete(Integer machineryTypeId) {
        //查询要删除的设备类型是否有设备
        int count = machineryTypeMapper.selectMachineryCount(machineryTypeId);
        if(count > 0){
            throw new RuntimeException("该设备类型下有设备,不能删除");
        }
        //查询要删除的设备类型下是否有子设备类型
        int childCount = machineryTypeMapper.selectChildrenCount(machineryTypeId);
        if(childCount > 0){
            throw new RuntimeException("该设备类型下有子设备类型,不能删除");
        }

        //删除设备类型
        machineryTypeMapper.delete(machineryTypeId);
    }


}
